//
//  HNSH_GoodsListViewController.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/2/24.
//

#define KTopViewHeight      48

#import "HNSH_GoodsListViewController.h"

//view
#import "HNSH_HomeGoodsCell.h"
#import "HNSH_GoodsListTopView.h"

//vc
#import "HNSH_GoodsDetailViewController.h"

@interface HNSH_GoodsListViewController ()

/* listAry */
@property (nonatomic, strong) NSMutableArray *listAry;

/* topView */
@property (nonatomic, strong) HNSH_GoodsListTopView *topView;

/* 排序类型 1=综合 2=销量 3=最新 4=价格 */
@property (nonatomic, copy) NSString *order_type;
/* 排序规则 1=升序 2=降序 */
@property (nonatomic, copy) NSString *order_rule;

@end

static NSString *HNSH_HomeGoodsCellID = @"HNSH_HomeGoodsCellID";

@implementation HNSH_GoodsListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self showNavTitle:@"商品列表" showLeftItem:YES];
    
    [self configUI];
}

- (void)configUI {
    self.order_type = @"1";
    self.order_rule = @"1";
    
    [self.view addSubview:self.topView];
    
    [self configCollectionView];
}

- (void)configCollectionView {
    [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(kNavBarAndStatusBarHeight+KTopViewHeight, 0, 0, 0));
    }];
    
    self.collectionView.backgroundColor = [UIColor colorWithHexString:@"#F9F9F9"];
    
    [self.collectionView registerClass:[HNSH_HomeGoodsCell class] forCellWithReuseIdentifier:HNSH_HomeGoodsCellID];
    
    //添加刷新
    [self setupRefresh:self.collectionView option:ST_RefreshDefault];
    //添加空页面
    [self setupEmpty:self.collectionView image:[UIImage imageNamed:@"empty_list_data"] title:@""];
}

#pragma mark networking
- (void)refreshData:(NSInteger)page {
    //分类传值
    NSString *categoryId = @"";
    if ([self.category_id isNotBlank]) {
        categoryId = self.category_id;
    }
    //搜索传值
    NSString *keyword = @"";
    if ([self.searchWord isNotBlank]) {
        keyword = self.searchWord;
    }
    [HNSH_ShopNetServer requestGoodsListWithShop_id:self.shop_id mcate_id:self.mcate_id scate_id:self.scate_id keyword:keyword sort_type:self.order_type sort_rule:self.order_rule type:self.type page_num:page success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            if (page == 1) {
                [self.listAry removeAllObjects];
            }
            NSArray *ary = [NSArray yy_modelArrayWithClass:[HNSH_GoodsListModel class] json:responseObject.resultset[@"list"]];
            [self.listAry addObjectsFromArray:ary];
            [self.collectionView reloadData];
            [self endRefresh:ary.count >= 10];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

#pragma mark lazy
- (HNSH_GoodsListTopView *)topView {
    if (!_topView) {
        _topView = [[HNSH_GoodsListTopView alloc]initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight, kScreenWidth, KTopViewHeight)];
        WeakSelf(weakSelf);
        _topView.typeBlock = ^(NSInteger typeIndex) {
            if (typeIndex >= 10) {
                weakSelf.order_type = [NSString stringWithFormat:@"%ld",typeIndex/10];
                weakSelf.order_rule = @"2";
            } else {
                weakSelf.order_type = [NSString stringWithFormat:@"%ld",typeIndex];
                weakSelf.order_rule = @"1";
            }
            [weakSelf headerRefreshing];
        };
    }
    return _topView;
}

- (NSMutableArray *)listAry {
    if (!_listAry) {
        _listAry = [NSMutableArray arrayWithCapacity:0];
    }
    return _listAry;
}

#pragma mark UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.listAry.count;
}
//设置海报图片
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HNSH_HomeGoodsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:HNSH_HomeGoodsCellID forIndexPath:indexPath];
    cell.model = self.listAry[indexPath.row];
    return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    UICollectionReusableView *reusableView = [collectionView supplementaryViewForElementKind:NSStringFromClass(UICollectionReusableView.class) atIndexPath:indexPath];
    return reusableView;
}
#pragma mark UICollectionViewDelegateFlowLayout
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 12, 8, 12);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat width = (kScreenWidth-32)/2;
    return CGSizeMake(width, width+91);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 8;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 8;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeZero;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    return CGSizeZero;
}
#pragma mark UICollectionViewDataSource
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    HNSH_GoodsListModel *model = self.listAry[indexPath.row];
    HNSH_GoodsDetailViewController *vc = [[HNSH_GoodsDetailViewController alloc]init];
    vc.goodsId = model.uid;
    [self.navigationController pushViewController:vc animated:YES];
}

@end
